package com.example.whereismystuff.views;

import java.io.File;

import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.ItemDataManager;
import com.example.whereismystuff.classes.LoginDataBaseAdapter;
import com.example.whereismystuff.classes.UserDataManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AdminLogin extends Activity {
	EditText usernameInput, passwordInput;
	Button login;
	AlertDialog.Builder alertBox;
	Intent i;

	String username, password;

	UserDataManager userManager;
	LoginDataBaseAdapter loginDataBaseAdapter;


	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adminlogin);

		loginDataBaseAdapter=new LoginDataBaseAdapter(this);
		loginDataBaseAdapter=loginDataBaseAdapter.open();

		login = (Button) findViewById(R.id.a_login);
		usernameInput = (EditText) findViewById(R.id.a_usernameInput);
		passwordInput = (EditText) findViewById(R.id.a_passwordInput);
		alertBox = new AlertDialog.Builder(this);
	}

	/**
	 * Sets up a generic God login called admin with the username Admin and password Admin
	 * This is only for testing purposes
	 * @param v
	 */
	public void onAdminLogin(View v) {
		String storedPassword = loginDataBaseAdapter.getSingleEntry(usernameInput.getText().toString());
		if((passwordInput.getText().toString().equals("admin") && usernameInput.getText().toString().equals("admin")) || passwordInput.getText().toString().equals(storedPassword) && loginDataBaseAdapter.getSingleEntryAdmin(usernameInput.getText().toString()).equals("true")) {
			Toast.makeText(AdminLogin.this, "Login Successful", Toast.LENGTH_SHORT).show();
			try{
				Class c = Class.forName("com.example.whereismystuff.views.UserView");
				i = new Intent(getApplicationContext(), c);
				startActivity(i);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		else
			//TODO: Make Dialog Box
			Toast.makeText(AdminLogin.this, "Username and Password don't match!", Toast.LENGTH_SHORT).show();	
	}
}
